#include "bridget_communicator.h"

WMI_HResult 
bridget_communicator::save(int idComputer, CM_ResultCollection& resultCollection)
{
	CM_DBLiteManager dbManager;
	dbManager.initialize();
	WMI_HResult whres;
	CM_ResultCollection CMresultCollection;

    int res = dbManager.saveResultCollection(idComputer, resultCollection);

	return whres;
}

WMI_HResult 
bridget_communicator::save(const wstring& dc, const wstring& fqdn, vector<ADCM_Computer>& computers)
{
	CM_DBLiteManager dbManager;
	dbManager.initialize();

	ADCM_Domain adcmDomain(wstring(dc.c_str()), wstring(fqdn.c_str()));
	dbManager.saveDomain(adcmDomain);

	WMI_HResult whres;
	int res = 0;
	
	if(adcmDomain.getDomainID() > 0)
	{
		for(vector<ADCM_Computer>::iterator it = computers.begin(); it != computers.end(); it++)
		{
			whres.setHRESULT(HRESULT(dbManager.saveComputer(adcmDomain.getDomainID(), *it)));
		}
	}
	else
	{
		res = 1;
	}
	
	whres.setHRESULT((HRESULT(res)));

	return whres;
}

WMI_HResult 
bridget_communicator::getDifferences(int idComputer, CM_ResultCollection& resultCollection)
{
	CM_DBLiteManager dbManager;
	dbManager.initialize();

	CM_ResultCollection current;
	CM_ResultCollection beforeCurrent;

	dbManager.getLastCollection(idComputer, current);

	dbManager.getBeforeLastCollection(idComputer, beforeCurrent);

	WMI_Result<WMI_Process> currentProcess = current.getProcesses();

	WMI_Result<WMI_Process> beforeCurrentProcess = beforeCurrent.getProcesses();

	WMI_Result<WMI_Process> differencesProcess;

	bc_utils::fillDifferences(currentProcess, beforeCurrentProcess, differencesProcess);

	for(int i = 0; i < differencesProcess.getObjects().size(); i++)
	{
		resultCollection.getProcesses().getObjects().push_back(differencesProcess.getObjects()[i]);
	}
	


	WMI_Result<WMI_Share> currentShares = current.getShares();
	WMI_Result<WMI_Share> beforeCurrentShares = beforeCurrent.getShares();
	WMI_Result<WMI_Share> differencesShares;

	bc_utils::fillDifferences(currentShares, beforeCurrentShares, differencesShares);


	for(int i = 0; i < differencesShares.getObjects().size(); i++)
	{
		resultCollection.getShares().getObjects().push_back(differencesShares.getObjects()[i]);
	}


	/*WMI_Result<WMI_Disk> currentDisks = current.getDisks();
	WMI_Result<WMI_Disk> beforeCurrentDisks = beforeCurrent.getDisks();
	WMI_Result<WMI_Disk> differencesDisks;

	fillDifferences(currentDisks, beforeCurrentDisks, differencesDisks);
	resultCollection.setDisks(differencesDisks);*/

	WMI_Result<WMI_FileDirectory> currentFileDirs = current.getFileDirs();
	WMI_Result<WMI_FileDirectory> beforeCurrentFileDirs = beforeCurrent.getFileDirs();
	WMI_Result<WMI_FileDirectory> differencesFileDirs;

	bc_utils::fillDifferences(currentFileDirs, beforeCurrentFileDirs, differencesFileDirs);

	for(int i = 0; i < differencesFileDirs.getObjects()->getContent().size(); i++)
	{
		resultCollection.getFileDirs().getObjects()->getContent().push_back(differencesFileDirs.getObjects()->getContent()[i]);
	}

	WMI_Result<WMI_Registry> currentRegistries = current.getRegistries();
	WMI_Result<WMI_Registry> beforeCurrentRegistries = beforeCurrent.getRegistries();
	WMI_Result<WMI_Registry> differencesRegistries;

	bc_utils::fillDifferencesReg(currentRegistries, beforeCurrentRegistries, differencesRegistries);

	for(int i = 0; i < differencesRegistries.getObjects().size(); i++)
	{
		resultCollection.getRegistries().getObjects().push_back(differencesRegistries.getObjects()[i]);
	}

	WMI_HResult res;

	return res;
}

WMI_HResult 
bridget_communicator::getComputers(wstring& dc, wstring& fqdn, wstring& idDomain, wstring& user, 
			 wstring& password, vector<ADCM_Computer>& computers)
{
	ADCM_Domain adcmDomain(dc, fqdn);
	WMI_HResult whres;
	int res = adcmDomain.loadComputers(user, password, computers);
	whres.setHRESULT(HRESULT(res));

	return whres;
}

WMI_HResult 
bridget_communicator::getComputersResult(int idComputer, CM_ResultCollection& resultCollection, wstring& user, 
											wstring& password, wstring& host, wstring& fqdn, bool localActive)
{
	CM_DBLiteManager dbManager;
	dbManager.initialize();
	WMI_HResult whres;

	wstring userName;
    userName.append(fqdn);
    userName.append(L"\\");
    userName.append(user);

	bc_utils::getProcess(resultCollection, userName, password, host, false, localActive);
	bc_utils::getShares(resultCollection, userName, password, host, false, localActive);
	bc_utils::getRegistries(RSOFTWAREPATH, resultCollection, userName, password, host, true, localActive);
	bc_utils::getFileDirectory(WINDOWS_DIR,resultCollection, userName, password, host, false, localActive);

	/*CMresultCollection.setShares(resultCollection.getShares());
	CMresultCollection.setProcesses(resultCollection.getProcesses());
	CMresultCollection.setRegistries(resultCollection.getRegistries());
	CMresultCollection.setFileDirs(resultCollection.getFileDirs());*/

	//int res = dbManager.getLastCollection(idComputer, resultCollection);

	/*resultCollection.setShares(CMresultCollection.getShares());
	resultCollection.setProcesses(CMresultCollection.getProcesses());
	resultCollection.setRegistries(CMresultCollection.getRegistries());
	resultCollection.setFileDirs(CMresultCollection.getFileDirs());*/

	whres.setHRESULT(HRESULT(0));

	return whres;
}

WMI_HResult 
bridget_communicator::getComputers(const wstring fqdn, vector<ADCM_Computer>& computersDB)
{
	CM_DBLiteManager dbManager;
	dbManager.initialize();

	WMI_HResult whres;

	int res = dbManager.getComputers(fqdn, computersDB);

	whres.setHRESULT(HRESULT(res));

	return whres;
}